<?php
/**
 * Create by 11li
 * Date 2020/2/10
 * Time 11:24
 *
 */

namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\service\CommissionService;
use app\common\service\PromoterService;

class Promoter extends Api
{
	
	/**
	 * 我的资料
	 */
	public function info() {
		$service = new PromoterService();
		$info = $service->getPromoterInfo($this->auth->id);
		if($info) {
			$this->success('查询成功', $info);
		} else {
			$this->error($service->getError());
		}
	}
	
	
	/**
	 * 我的推广商品
	 */
	public function myGoods() {
		$service = new PromoterService();
		$goods = $service->getPromoterGoods($this->auth->id);
		
		$this->success('查询成功', $goods);
	}
	
	/**
	 * 保存推广商品
	 */
	public function saveShareGoods() {
		$postData = $this->request->post();
		$goodsIds = $postData['goods_ids'];
		
		$service = new PromoterService();
		$res = $service->savePromoterGoods($this->auth->id, $goodsIds);
		if($res) {
			$this->success('保存成功');
		} else {
			$this->error($service->getError());
		}
	}
	
	/**
	 * 我的粉丝
	 */
	public function fans($page = 1)
	{
		$obj = new PromoterService();
		$result = $obj->getFansByUserId($this->auth->id, $page);
		$this->success('查询成功', $result);
	}
	
	/**
	 * 我推广的订单
	 */
	public function recommendOrder($page = 1) {
		$obj = new PromoterService();
		$result = $obj->getRecommendOrderByUserId($this->auth->id, $page);
		$this->success('查询成功', $result);
	}
	
	/**
	 * 我的访客
	 */
	public function visitor($page = 1)
	{
		$obj = new PromoterService();
		$result = $obj->getVisitorByUserId($this->auth->id, $page);
		$this->success('查询成功', $result);
	}
	
	
	/**
	 * 我的资料
	 */
	public function promoterDetail($user_id) {
		$service = new PromoterService();
		$info = $service->getPromoterDetail($user_id);
		if($info) {
			$this->success('查询成功', $info);
		} else {
			$this->error($service->getError());
		}
	}
	
	
	
	/**
	 * 所有的商品
	 */
	public function promoterGoods()
	{
		$userId = $this->request->get('user_id', '', 'intval');
		$page = $this->request->get('page', '', 'intval');
		$size = $this->request->get('size', 15, 'intval');
		if(!$userId || !$page) {
			$this->error('参数错误');
		}
		
		$obj = new PromoterService();
		$res = $obj->getPromoterGoodsList($userId, $page, $size);
		$this->success('查询成功', $res);
	}
	
	/**
	 * 添加浏览记录佣金
	 */
	public function addShareRecord() {
		$shareUserId = $this->request->get('user_id', '', 'intval');
		$goodsId = $this->request->get('goods_id', '', 'intval');
		
		if(!$shareUserId || !$goodsId) {
			$this->error('参数错误');
		}
		$viewUserId = $this->auth->id;
		
		$obj = new CommissionService();
		$res = $obj->addShareCommission($viewUserId, $shareUserId, $goodsId);
		if($res) {
			$this->success('添加成功');
		} else {
			$this->error($obj->getError());
		}
	}
}